'use client';

import { queryOauthClientById } from '@/services/api';
import { useQuery } from '@tanstack/react-query';
import type { IOauthClient } from '@/interfaces';
import Alert from '@/app/[locale]/alert/alert';
import { useContext } from 'react';
import AlertLoad from '@/app/[locale]/alert/load';
import { OauthClientAdminContext } from '@/contexts/admin/oauth/client';
import QueryOauthClientInfo from '@/app/[locale]/admin/oauth/client/detail/info';
import OauthClientInterfaceAdminPage from '@/app/[locale]/admin/oauth/client/interface';

export default function DetailsOauthClientAdminPage() {
  const context = useContext(OauthClientAdminContext);

  const queryOauthClientsByIdQuery = useQuery(
    ['/oauth', '/clients', context.detailItem?.id],
    async (context) => {
      return (await queryOauthClientById({
        id: context.queryKey[2] + '',
      })) as IOauthClient;
    },
  );

  if (queryOauthClientsByIdQuery.data) {
    const data = queryOauthClientsByIdQuery.data;

    return (
      <div className="row">
        <div className="col">
          <div className="vstack gap-4">
            <QueryOauthClientInfo details={data} />

            <OauthClientInterfaceAdminPage />

            <div></div>
          </div>
        </div>
      </div>
    );
  }

  if (queryOauthClientsByIdQuery.error) {
    return <Alert type="error" message={queryOauthClientsByIdQuery.error} />;
  }

  return <AlertLoad />;
}
